<?php
/**
 * Created by PhpStorm.
 * User: wangjie
 * Date: 2020/07/27
 * Time: 下午5:13
 */

namespace App\Api\Controllers\Gtxy;


use App\Api\Controllers\Config\LinkageConfigController;
use App\Api\Controllers\Config\MyBankConfigController;
use App\Http\Controllers\Controller;
use App\Models\MemberList;
use App\Models\MemberSetJf;
use App\Models\MemberTpl;
use EasyWeChat\Factory;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Log;

class OauthController extends Controller
{

    //授权
    public function oauth(Request $request)
    {
        $sub_info = $request->get('state');
        $sub_info_arr = json_decode(base64_decode((string)$sub_info), true);
        $bank_type = $sub_info_arr['bank_type'];
        $version = "gtxy";
        if ($bank_type == "gtxy_v2_weixin") {
            $version = "gtxy_v2";
        }
        Log::channel("bug")->info("oauth",[
            "sub_info"=>$sub_info,
            "version"=>$version,
        ]);
        return redirect(url('api/gtxy/weixin/oauth_callback?sub_info=' . $sub_info . '&wx_AppId=111' . '&wx_Secret=111' . '&version=' . $version));
    }


    public function oauth_callback(Request $request)
    {
        $sub_info = $request->get('sub_info');

        $sub_info_arr = json_decode(base64_decode((string)$sub_info), true);
        $store_id = $sub_info_arr['store_id'];
        $store_name = $sub_info_arr['store_name'];
        $merchant_id = $sub_info_arr['merchant_id'];
        $version = $request->get('version');

        $data = [
            'store_id' => $store_id,
            'store_name' => $store_name,
            'store_address' => '',
            'open_id' => 11,
            'merchant_id' => $merchant_id,
            "version" => $version
        ];

        $data = base64_encode(json_encode((array)$data));

        return redirect('/api/gtxy/weixin/pay_view?data=' . $data);
    }

    public function unionpay(Request $request)
    {
        $sub_info = $request->get('state');
        $sub_info_arr = json_decode(base64_decode((string)$sub_info), true);
        $store_id = $sub_info_arr['store_id'];
        $bank_type = $sub_info_arr['bank_type'];
        $version = "gtxy";
        if ($bank_type == "gtxy_v2") {
            $version = "gtxy_v2";
        }
        $store_pid = 0;
        $data = [
            'store_id' => $sub_info_arr['store_id'],
            'store_name' => $sub_info_arr['store_name'],
            'store_address' => '',
            'open_id' => 11,
            'merchant_id' => $sub_info_arr['merchant_id'],
            "version" => $version
        ];
        if ($version == "gtxy") {
            return view('gtxy.unionpay', compact('data'));
        } else {
            return view('gtxy_v2.unionpay', compact('data'));
        }
    }


    //支付显示页面
    public function pay_view(Request $request)
    {
        $data = json_decode(base64_decode((string)$request->get('data')), true);

        $version = $data['version'];

        if ($version == "gtxy") {
            return view('gtxy.weixin', compact('data'));
        } else {
            return view('gtxy_v2.weixin', compact('data'));
        }
    }


}
